public class MyLinkList implements IList {

    static class ListNode {
        int val;
        ListNode prev;
        ListNode next;

        ListNode(int data) {
            this.val = data;
        }
    }

    private ListNode head;
    private ListNode last;

    @Override
    public void addFirst(int data) {
        ListNode newNode = new ListNode(data);
        if (this.head == null) {
            this.head = newNode;
            this.last = newNode;
        } else {
            this.head.prev = newNode;
            newNode.next = this.head;
            this.head = newNode;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode newNode = new ListNode(data);
        if (this.head == null) {
            this.head = newNode;
            this.last = newNode;
        } else {
            newNode.prev = this.last;
            this.last.next = newNode;
            this.last = newNode;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        int len = size();
        if (index < 0 || index > len) {
            //抛出自定义异常
            return;
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == len) {
            addLast(data);
            return;
        }

        int count = 0;
        ListNode cur = this.head;
        while (count < index - 1) {
            cur = cur.next;
            count++;
        }

        ListNode newNode = new ListNode(data);
        cur.next.prev = newNode;
        newNode.next = cur.next;
        cur.next = newNode;
        newNode.prev = cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        ListNode cur = head;
        while(cur!=null) {
            if(cur.val == key) {
                if(cur == head) {
                    //删除头节点
                    head = head.next;
                    if(head==null) {
                        last = null;
                    }else {
                        head.prev = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if(cur.next == null) {
                        //删除尾节点
                        last = last.prev;
                    }else {
                        cur.next.prev = cur.prev;
                    }
                }
                return;
            }
            cur = cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur = head;
        while(cur!=null) {
            if(cur.val == key) {
                if(cur == head) {
                    //删除头节点
                    head = head.next;
                    if(head==null) {
                        last = null;
                    }else {
                        head.prev = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if(cur.next == null) {
                        //删除尾节点
                        last = last.prev;
                    }else {
                        cur.next.prev = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }

    @Override
    public int size() {
        ListNode cur = head;
        int count = 0;
        while (cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }

    @Override
    public void clear() {
        ListNode cur = this.head;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = curNext;
        }
        this.head = null;
        this.last = null;
    }

    @Override
    public void display() {
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + "->");
            cur = cur.next;
        }
        System.out.println("nullptr");
    }
}
